package org.codeart.decorator.loader;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class EncryptDecorator extends DataLoaderDecorator {

    public EncryptDecorator(DataLoader dataLoader) {
        super(dataLoader);
    }

    @Override
    public String read() {
        return decode(super.read());
    }

    @Override
    public void write(String data) {
        super.write(encode(data));
    }
    
    private String encode(String data) {
        return Base64.getEncoder().encodeToString(data.getBytes(StandardCharsets.UTF_8));
    }
    
    private String decode(String data) {
        byte[] bytes = Base64.getDecoder().decode(data);
        return new String(bytes, StandardCharsets.UTF_8);
    }
}
